home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Applications / gdbm-1.7.3 / source / gdbmopen.c < prev    next >
Text File  |  1994-05-22  |  13KB  |  460 lines

  1. /* gdbmopen.c - Open the dbm file and initialize data structures for use. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.        
  27. *************************************************************************/
  28.  
  29.  
  30. /* AIX demands this be the very first thing in the file. */
  31. #if !defined(__GNUC__) && defined(_AIX)
  32.  #pragma alloca
  33. #endif
  34.  
  35. /* include system configuration before all else. */
  36. #include "autoconf.h"
  37.  
  38. #include "gdbmdefs.h"
  39. #include "gdbmerrno.h"
  40.  
  41. /* Initialize dbm system.  FILE is a pointer to the file name.  If the file
  42.    has a size of zero bytes, a file initialization procedure is performed,
  43.    setting up the initial structure in the file.  BLOCK_SIZE is used during
  44.    initialization to determine the size of various constructs.  If the value
  45.    is less than 512, the file system blocksize is used, otherwise the value
  46.    of BLOCK_SIZE is used.  BLOCK_SIZE is ignored if the file has previously
  47.    initialized.  If FLAGS is set to GDBM_READ the user wants to just
  48.    read the database and any call to dbm_store or dbm_delete will fail. Many
  49.    readers can access the database at the same time.  If FLAGS is set to
  50.    GDBM_WRITE, the user wants both read and write access to the database and
  51.    requires exclusive access.  If FLAGS is GDBM_WRCREAT, the user wants
  52.    both read and write access to the database and if the database does not
  53.    exist, create a new one.  If FLAGS is GDBM_NEWDB, the user want a
  54.    new database created, regardless of whether one existed, and wants read
  55.    and write access to the new database.  Any error detected will cause a 
  56.    return value of null and an approprate value will be in gdbm_errno.  If
  57.    no errors occur, a pointer to the "gdbm file descriptor" will be
  58.    returned. */
  59.    
  60.  
  61. gdbm_file_info *
  62. gdbm_open (file, block_size, flags, mode, fatal_func)
  63.      char *file;
  64.      int  block_size;
  65.      int  flags;
  66.      int  mode;
  67.      void (*fatal_func) ();
  68. {
  69.   gdbm_file_info *dbf;        /* The record to return. */
  70.   struct stat file_stat;    /* Space for the stat information. */
  71.   int         len;        /* Length of the file name. */
  72.   int         num_bytes;    /* Used in reading and writing. */
  73.   off_t       file_pos;        /* Used with seeks. */
  74.   int          lock_val;         /* Returned by the flock call. */
  75.   int          file_block_size;    /* Block size to use for a new file. */
  76.   int           index;        /* Used as a loop index. */
  77.   char        need_trunc;    /* Used with GDBM_NEWDB and locking to avoid
  78.                    truncating a file from under a reader. */
  79.  
  80.   /* Initialize the gdbm_errno variable. */
  81.   gdbm_errno = GDBM_NO_ERROR;
  82.  
  83.   /* Allocate new info structure. */
  84.   dbf = (gdbm_file_info *) malloc (sizeof (gdbm_file_info));
  85.   if (dbf == NULL)
  86.     {
  87.       gdbm_errno = GDBM_MALLOC_ERROR;
  88.       return NULL;
  89.     }
  90.  
  91.   /* Initialize some fields for known values.  This is done so gdbm_close
  92.      will work if called before allocating some structures. */
  93.   dbf->dir  = NULL;
  94.   dbf->bucket = NULL;
  95.   dbf->header = NULL;
  96.   dbf->bucket_cache = NULL;
  97.   dbf->cache_size = 0;
  98.   
  99.   /* Save name of file. */
  100.   len = strlen (file);
  101.   dbf->name = (char *) malloc (len + 1);
  102.   if (dbf->name == NULL)
  103.     {
  104.       free (dbf);
  105.       gdbm_errno = GDBM_MALLOC_ERROR;
  106.       return NULL;
  107.     }
  108.   strcpy (dbf->name, file);
  109.  
  110.   /* Initialize the fatal error routine. */
  111.   dbf->fatal_err = fatal_func;
  112.  
  113.   /* Check for fast writers. */
  114.   if (flags & GDBM_FAST)
  115.     {
  116.       dbf->fast_write = TRUE;
  117.       flags -= GDBM_FAST;
  118.     }
  119.   else
  120.     {
  121.       dbf->fast_write = FALSE;
  122.     }
  123.   
  124.   /* Open the file. */
  125.   need_trunc = FALSE;
  126.   if (flags == GDBM_READER)
  127.     {
  128. #ifdef THINK_C
  129.       dbf->desc = open (dbf->name, O_RDONLY);
  130. #else
  131.       dbf->desc = open (dbf->name, O_RDONLY, 0);
  132. #endif
  133.     }
  134.   else if (flags == GDBM_WRITER)
  135.     {
  136. #ifdef THINK_C
  137.       dbf->desc = open (dbf->name, O_RDWR);
  138. #else    
  139.       dbf->desc = open (dbf->name, O_RDWR, 0);
  140. #endif
  141.     }
  142.   else if (flags == GDBM_NEWDB)
  143.     {
  144. #ifdef THINK_C
  145.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT);
  146. #else
  147.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT, mode);
  148. #endif
  149.       flags = GDBM_WRITER;
  150.       need_trunc = TRUE;
  151.     }
  152.   else
  153.     {
  154. #ifdef THINK_C
  155.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT);
  156. #else
  157.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT, mode);
  158. #endif
  159.       flags = GDBM_WRITER;
  160.     }
  161.   if (dbf->desc < 0)
  162.     {
  163.       free (dbf->name);
  164.       free (dbf);
  165.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  166.       return NULL;
  167.     }
  168.  
  169.   /* Get the status of the file. */
  170.   fstat (dbf->desc, &file_stat);
  171.  
  172.   /* Lock the file in the approprate way. */
  173.   if (flags == GDBM_READER)
  174.     {
  175.       if (file_stat.st_size == 0)
  176.     {
  177.       close (dbf->desc);
  178.       free (dbf->name);
  179.       free (dbf);
  180.       gdbm_errno = GDBM_EMPTY_DATABASE;
  181.       return NULL;
  182.     }
  183.       /* Sets lock_val to 0 for success.  See systems.h. */
  184.       READLOCK_FILE(dbf);
  185.     }
  186.   else
  187.     {
  188.       /* Sets lock_val to 0 for success.  See systems.h. */
  189.       WRITELOCK_FILE(dbf);
  190.     }
  191.   if (lock_val != 0)
  192.     {
  193.       close (dbf->desc);
  194.       free (dbf->name);
  195.       free (dbf);
  196.       if (flags == GDBM_READER)
  197.     gdbm_errno = GDBM_CANT_BE_READER;
  198.       else
  199.     gdbm_errno = GDBM_CANT_BE_WRITER;
  200.       return NULL;
  201.     }
  202.  
  203.   /* Record the kind of user. */
  204.   dbf->read_write = flags;
  205.  
  206.   /* If we do have a write lock and it was a GDBM_NEWDB, it is 
  207.      now time to truncate the file. */
  208.   if (need_trunc && file_stat.st_size != 0)
  209.     {
  210.       TRUNCATE (dbf);
  211.       fstat (dbf->desc, &file_stat);
  212.     }
  213.  
  214.   /* Decide if this is a new file or an old file. */
  215.   if (file_stat.st_size == 0)
  216.     {
  217.  
  218.       /* This is a new file.  Create an empty database.  */
  219.  
  220.       /* Start with the blocksize. */
  221.       if (block_size < 512)
  222.     file_block_size = STATBLKSIZE;
  223.       else
  224.     file_block_size = block_size;
  225.  
  226.       /* Get space for the file header. */
  227.       dbf->header = (gdbm_file_header *) malloc (file_block_size);
  228.       if (dbf->header == NULL)
  229.     {
  230.       gdbm_close (dbf);
  231.       gdbm_errno = GDBM_MALLOC_ERROR;
  232.       return NULL;
  233.     }
  234.  
  235.       /* Set the magic number and the block_size. */
  236.       dbf->header->header_magic = 0x13579ace;
  237.       dbf->header->block_size = file_block_size;
  238.      
  239.       /* Create the initial hash table directory.  */
  240.       dbf->header->dir_size = 8 * sizeof (off_t);
  241.       dbf->header->dir_bits = 3;
  242.       while (dbf->header->dir_size < dbf->header->block_size)
  243.     {
  244.       dbf->header->dir_size <<= 1;
  245.       dbf->header->dir_bits += 1;
  246.     }
  247.  
  248.       /* Check for correct block_size. */
  249.       if (dbf->header->dir_size != dbf->header->block_size)
  250.     {
  251.       gdbm_close (dbf);
  252.       gdbm_errno = GDBM_BLOCK_SIZE_ERROR;
  253.       return NULL;
  254.     }
  255.  
  256.       /* Allocate the space for the directory. */
  257.       dbf->dir = (off_t *) malloc (dbf->header->dir_size);
  258.       if (dbf->dir == NULL)
  259.     {
  260.       gdbm_close (dbf);
  261.       gdbm_errno = GDBM_MALLOC_ERROR;
  262.       return NULL;
  263.     }
  264.       dbf->header->dir = dbf->header->block_size;
  265.  
  266.       /* Create the first and only hash bucket. */
  267.       dbf->header->bucket_elems =
  268.     (dbf->header->block_size - sizeof (hash_bucket))
  269.     / sizeof (bucket_element) + 1;
  270.       dbf->header->bucket_size  = dbf->header->block_size;
  271. #if !defined(sgi)
  272.       dbf->bucket = (hash_bucket *) (alloca (dbf->header->bucket_size));
  273. #else    /* sgi */
  274.       /* The SGI C compiler doesn't accept the previous form. */
  275.       {
  276.         hash_bucket *ptr;
  277.     ptr = (hash_bucket *) (alloca (dbf->header->bucket_size));
  278.     dbf->bucket = ptr;
  279.       }
  280. #endif    /* sgi */
  281.       if (dbf->bucket == NULL)
  282.     {
  283.       gdbm_close (dbf);
  284.       gdbm_errno = GDBM_MALLOC_ERROR;
  285.       return NULL;
  286.     }
  287.       _gdbm_new_bucket (dbf, dbf->bucket, 0);
  288.       dbf->bucket->av_count = 1;
  289.       dbf->bucket->bucket_avail[0].av_adr = 3*dbf->header->block_size;
  290.       dbf->bucket->bucket_avail[0].av_size = dbf->header->block_size;
  291.  
  292.       /* Set table entries to point to hash buckets. */
  293.       for (index = 0; index < dbf->header->dir_size / sizeof (off_t); index++)
  294.     dbf->dir[index] = 2*dbf->header->block_size;
  295.  
  296.       /* Initialize the active avail block. */
  297.       dbf->header->avail.size
  298.     = ( (dbf->header->block_size - sizeof (gdbm_file_header))
  299.      / sizeof (avail_elem)) + 1;
  300.       dbf->header->avail.count = 0;
  301.       dbf->header->avail.next_block = 0;
  302.       dbf->header->next_block  = 4*dbf->header->block_size;
  303.  
  304.       /* Write initial configuration to the file. */
  305.       /* Block 0 is the file header and active avail block. */
  306.       num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  307.       if (num_bytes != dbf->header->block_size)
  308.     {
  309.       gdbm_close (dbf);
  310.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  311.       return NULL;
  312.     }
  313.  
  314.       /* Block 1 is the initial bucket directory. */
  315.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  316.       if (num_bytes != dbf->header->dir_size)
  317.     {
  318.       gdbm_close (dbf);
  319.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  320.       return NULL;
  321.     }
  322.  
  323.       /* Block 2 is the only bucket. */
  324.       num_bytes = write (dbf->desc, dbf->bucket, dbf->header->bucket_size);
  325.       if (num_bytes != dbf->header->bucket_size)
  326.     {
  327.       gdbm_close (dbf);
  328.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  329.       return NULL;
  330.     }
  331.  
  332.       /* Wait for initial configuration to be written to disk. */
  333.       fsync (dbf->desc);
  334.  
  335.     }
  336.   else
  337.     {
  338.       /* This is an old database.  Read in the information from the file
  339.      header and initialize the hash directory. */
  340.  
  341.       gdbm_file_header partial_header;  /* For the first part of it. */
  342.  
  343.       /* Read the partial file header. */
  344.       num_bytes = read (dbf->desc, &partial_header, sizeof (gdbm_file_header));
  345.       if (num_bytes != sizeof (gdbm_file_header))
  346.     {
  347.       gdbm_close (dbf);
  348.       gdbm_errno = GDBM_FILE_READ_ERROR;
  349.       return NULL;
  350.     }
  351.  
  352.       /* Is the magic number good? */
  353.       if (partial_header.header_magic != 0x13579ace)
  354.     {
  355.       gdbm_close (dbf);
  356.       gdbm_errno = GDBM_BAD_MAGIC_NUMBER;
  357.       return NULL;
  358.     }
  359.  
  360.       /* It is a good database, read the entire header. */
  361.       dbf->header = (gdbm_file_header *) malloc (partial_header.block_size);
  362.       if (dbf->header == NULL)
  363.     {
  364.       gdbm_close (dbf);
  365.       gdbm_errno = GDBM_MALLOC_ERROR;
  366.       return NULL;
  367.     }
  368.       bcopy (&partial_header, dbf->header, sizeof (gdbm_file_header));
  369.       num_bytes = read (dbf->desc, &dbf->header->avail.av_table[1],
  370.             dbf->header->block_size-sizeof (gdbm_file_header));
  371.       if (num_bytes != dbf->header->block_size-sizeof (gdbm_file_header))
  372.     {
  373.       gdbm_close (dbf);
  374.       gdbm_errno = GDBM_FILE_READ_ERROR;
  375.       return NULL;
  376.     }
  377.     
  378.       /* Allocate space for the hash table directory.  */
  379.       dbf->dir = (off_t *) malloc (dbf->header->dir_size);
  380.       if (dbf->dir == NULL)
  381.     {
  382.       gdbm_close (dbf);
  383.       gdbm_errno = GDBM_MALLOC_ERROR;
  384.       return NULL;
  385.     }
  386.  
  387.       /* Read the hash table directory. */
  388.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  389.       if (file_pos != dbf->header->dir)
  390.     {
  391.       gdbm_close (dbf);
  392.       gdbm_errno = GDBM_FILE_SEEK_ERROR;
  393.       return NULL;
  394.     }
  395.  
  396.       num_bytes = read (dbf->desc, dbf->dir, dbf->header->dir_size);
  397.       if (num_bytes != dbf->header->dir_size)
  398.     {
  399.       gdbm_close (dbf);
  400.       gdbm_errno = GDBM_FILE_READ_ERROR;
  401.       return NULL;
  402.     }
  403.  
  404.     }
  405.       
  406.   /* Finish initializing dbf. */
  407.   dbf->last_read = -1;
  408.   dbf->bucket = NULL;
  409.   dbf->bucket_dir = 0;
  410.   dbf->cache_entry = NULL;
  411.   dbf->header_changed = FALSE;
  412.   dbf->directory_changed = FALSE;
  413.   dbf->bucket_changed = FALSE;
  414.   dbf->second_changed = FALSE;
  415.   
  416.   /* Everything is fine, return the pointer to the file
  417.      information structure.  */
  418.   return dbf;
  419.  
  420. }
  421.  
  422. /* initialize the bucket cache. */
  423. int
  424. _gdbm_init_cache(dbf, size)
  425.     gdbm_file_info *dbf;
  426.     int size;
  427. {
  428. register int index;
  429.  
  430.   if (dbf->bucket_cache == NULL)
  431.     {
  432.       dbf->bucket_cache = (cache_elem *) malloc(sizeof(cache_elem) * size);
  433.       if(dbf->bucket_cache == NULL)
  434.         {
  435.           gdbm_errno = GDBM_MALLOC_ERROR;
  436.           return(-1);
  437.         }
  438.       dbf->cache_size = size;
  439.  
  440.       for(index = 0; index < size; index++)
  441.         {
  442.           (dbf->bucket_cache[index]).ca_bucket
  443.             = (hash_bucket *) malloc (dbf->header->bucket_size);
  444.           if ((dbf->bucket_cache[index]).ca_bucket == NULL)
  445.         {
  446.               gdbm_errno = GDBM_MALLOC_ERROR;
  447.           return(-1);
  448.             }
  449.           (dbf->bucket_cache[index]).ca_adr = 0;
  450.           (dbf->bucket_cache[index]).ca_changed = FALSE;
  451.           (dbf->bucket_cache[index]).ca_data.hash_val = -1;
  452.           (dbf->bucket_cache[index]).ca_data.elem_loc = -1;
  453.           (dbf->bucket_cache[index]).ca_data.dptr = NULL;
  454.         }
  455.       dbf->bucket = dbf->bucket_cache[0].ca_bucket;
  456.       dbf->cache_entry = &dbf->bucket_cache[0];
  457.     }
  458.   return(0);
  459. }
  460.